package com.autodatabase.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Test2 {
	 private Connection con = null ;
	    private Statement stmt = null ;
	    private String url = "jdbc:mysql://localhost:3306/jeecmsv5";
	    private String user = "root";
	    private String pwd = "root123";
	   
	    public Test2() {
	        init();
	    }
	    
	    private void init(){
	        try {       
	          Class.forName("com.mysql.jdbc.Driver").newInstance();
	          con = DriverManager.getConnection(url ,user ,pwd );
	          stmt = con .createStatement();
	        } catch (Exception e){
	          // your installation of JDBC Driver Failed
	          e.printStackTrace();
	        }
	    }
	    
	    public void add(String sn,String ss,String sa,String so){    
	        String sql2 = "insert into student value ('"
	                +sn+"',' "+ss+"',' "+sa+"',` "+so+"` ); ";
	        try {
	            stmt.execute(sql2);
	        }catch (SQLException e){
	            e.printStackTrace();
	        }
	    }
	    
	    public String search(String sn){
	        String str = "so ：sa ："+sn+" ss ：";
	        String sql1 = "select * from student where sn= '"+sn+"'; ";
	        try {
	            ResultSet rs = stmt .executeQuery(sql1);          
	            if(rs.next()){  
	                str = str+rs.getString("sn ")+" 手机号 ："
	                        +rs.getString("sa ");
	            }else str = "该记录不存在 ！！！";
	        }catch (Exception e){
	           e.printStackTrace();
	        }
	        return str;
	    }
	    
	    public void modify(String sn,String ss){
	        String sql = "update sturesult set ss ="+ss
	                +" where sn ='"+sn+"' ";
	        try {
	            stmt .executeUpdate(sql);
	        }catch (SQLException e){
	            e.printStackTrace();
	        }
	    }
	    
	    public void delete(String sn){
	        String sql1 = "delete from teststudent "+" where sn= '"+sn+"'; ";
	        try {
	            stmt.executeUpdate(sql1);
	        }catch (Exception e){
	            e.printStackTrace();
	        }
	    }
	           
	    public void close(){
	        try {
	            if (con != null ) con .close();
	            if (stmt != null ) stmt .close();
	        }catch (Exception e){
	            e.printStackTrace();
	        }
	    }
	    public static void main(String args[])throws Exception{
	    	Test2 t = new Test2();
	        //add test here!!!!
	    	System.out .println("ghost is a big worry "); 
	    }
}
